import React from 'react';
import { Alert } from 'react-native';
import { Scene, Actions } from 'react-native-router-flux';
import Form from './Form';
import members from '../models/members';
import policy from '../models/policies';
import navBarStyle from '../../../components/navBarStyle';


const alert = Alert.alert;

const pressHandle = (values) => {
  if (!values.policyId) {
    policy.create(values).then(data => {
      if (data) {
        alert('操作成功', '添加家庭保单', [
          {
            text: '继续添加保单',
          },
          {
            text: '返回',
            onPress: Actions.pop
          }
        ]);
      }
    });
  } else {
    policy.update(values).then(data => {
      if (data) {
        alert('操作成功', '更新家庭保单', [
          {
            text: '返回',
            onPress: Actions.pop
          }
        ]);
      }
    });;
  }
};

module.exports = [
  <Scene
    key="editFamilyPolicy"
    component={Form}
    path="edit/family/policy/:customerId/:id"
    title="更新家庭保单"
    hideTabBar
    onSubmit={pressHandle}
    members={members}
    policy={policy}
    {...navBarStyle}
    onEnter={({ customerId, id }) => {
      policy.initEdit(customerId, id);
    }}
    onExit={policy.reset}
  />,
  <Scene
    key="createFamilyPolicy"
    component={Form}
    path="create/family/policy/:customerId"
    title="新增家庭保单"
    hideTabBar
    onSubmit={pressHandle}
    members={members}
    policy={policy}
    {...navBarStyle}
    onEnter={({ customerId }) => {
      policy.initEdit(customerId);
    }}
  />
];
